package org.xlx.xlx_oa.warehouse.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.common.enums.OperationEnums;
import org.xlx.xlx_oa.warehouse.convertor.WarehouseConvertor;
import org.xlx.xlx_oa.warehouse.domain.WarehouseDistributor;
import org.xlx.xlx_oa.warehouse.domain.dto.DistributorDto;
import org.xlx.xlx_oa.warehouse.domain.vo.DistributorVo;
import org.xlx.xlx_oa.warehouse.mapper.WarehouseDistributorMapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class WarehouseDistributorService {
    private final WarehouseConvertor warehouseConvertor = WarehouseConvertor.INSTANCE;
    @Autowired
    private WarehouseDistributorMapper distributorMapper;

    /**
     * 保存供应商信息
     */
    public Boolean saveDistributor(DistributorVo distributorVo) {
        WarehouseDistributor distributor = warehouseConvertor.convertVoToDistributor(distributorVo);
        Long id = distributorVo.getId();
        if (OperationEnums.Termination.name().equals(distributor.getCooperationStatus())) {
            if (Objects.isNull(distributor.getClosureReason())) {
                throw new BusinessException("100012", "There must be reasons for termination of cooperation");
            }
        }
        if (Objects.isNull(id)) {
            distributorMapper.insert(distributor);
        } else {
            distributorMapper.updateById(distributor);
        }
        return true;
    }

    /**
     * 删除供应商信息
     */
    public Boolean removeDistributor(Long id) {
        WarehouseDistributor distributor = distributorMapper.selectById(id);
        if (OperationEnums.Cooperation.name().equals(distributor.getCooperationStatus())){
            throw new BusinessException("100014","Cooperation status, cannot be deleted");
        }
        return distributorMapper.deleteById(id) > 0;
    }

    /**
     * 供应商信息列表
     */
    public List<DistributorDto> getList(DistributorVo distributorVo) {
        WarehouseDistributor distributor = warehouseConvertor.convertVoToDistributor(distributorVo);
        Page<WarehouseDistributor> page = new Page<>(distributorVo.getPageNum(), distributorVo.getPageSize());
        QueryWrapper<WarehouseDistributor> queryWrapper = new QueryWrapper<>();
        if (distributor.getCompanyName()!=null&&(!"".equals(distributor.getCompanyName()))) {
            queryWrapper.lambda().like(WarehouseDistributor::getCompanyName, distributor.getCompanyName());
        }
        if (distributor.getCooperationStatus()!=null&&(!"".equals(distributor.getCooperationStatus()))) {
            queryWrapper.lambda().eq(WarehouseDistributor::getCooperationStatus, distributor.getCooperationStatus());
        }
        distributorMapper.selectPage(page, queryWrapper);
        List<DistributorDto> distributorDtos = warehouseConvertor.convertToListDistributorDto(page.getRecords());
        for (DistributorDto distributorDto : distributorDtos) {
            String status = this.getTextByName(distributorDto.getCooperationStatus());
            distributorDto.setCooperationStatusName(status);
        }
        return distributorDtos;
    }

    /**
     * 经销商总数
     * @param distributorVo
     * @return
     */
    public Integer getCount(DistributorVo distributorVo){
        QueryWrapper<WarehouseDistributor> queryWrapper = new QueryWrapper<>();
        if (distributorVo.getCompanyName()!=null&&(!"".equals(distributorVo.getCompanyName()))) {
            queryWrapper.lambda().like(WarehouseDistributor::getCompanyName, distributorVo.getCompanyName());
        }
        if (distributorVo.getCooperationStatus()!=null&&(!"".equals(distributorVo.getCooperationStatus()))) {
            queryWrapper.lambda().eq(WarehouseDistributor::getCooperationStatus, distributorVo.getCooperationStatus());
        }
        return distributorMapper.selectCount(queryWrapper);
    }

    /**
     * 供应商信息详情
     */
    public DistributorDto getDetail(Long id) {
        WarehouseDistributor distributor = distributorMapper.selectById(id);
        DistributorDto distributorDto = warehouseConvertor.convertToDistributorDto(distributor);
        String status = this.getTextByName(distributorDto.getCooperationStatus());
        distributorDto.setCooperationStatusName(status);
        return distributorDto;
    }

    /**
     * 模糊查询
     * @param companyName
     * @return
     */
    public List<DistributorDto> searchByCompanyName(String companyName){
        QueryWrapper<WarehouseDistributor> queryWrapper = new QueryWrapper<>();
        if (companyName!=null&&(!"".equals(companyName))) {
            queryWrapper.lambda().like(WarehouseDistributor::getCompanyName, companyName);
        }
        List<WarehouseDistributor> warehouseDistributors = distributorMapper.selectList(queryWrapper);
        List<DistributorDto> distributorDtos = warehouseConvertor.convertToListDistributorDto(warehouseDistributors);
        for (DistributorDto distributorDto : distributorDtos) {
            String status = this.getTextByName(distributorDto.getCooperationStatus());
            distributorDto.setCooperationStatusName(status);
        }
        return distributorDtos;
    }

    /**
     * 获得合作状态code的文本
     *
     * @param name
     * @return
     */
    public String getTextByName(String name) {
        OperationEnums enums = OperationEnums.valueOf(OperationEnums.class, name);
        return enums.getText();
    }
}
